Ext.define('App.report.project.view.ProjectReportList', {
    extend: 'Extend.grid.CrudGridPanel',
    alias: 'widget.ProjectReportList',
    requires: ['App.report.project.model.ProjectReportViewModel'],
    viewModel: 'ProjectReportViewModel',
    border: false,
    sortableColumns: false,
    config: {
        tbar: {
            reCalculate: {
                text: '重新计算',
                handler: function () {
                    var me = this.up().up();
                    var records = me.getSelectedRecords();
                    if (records.length != 1) {
                        Extend.Msg.error("错误", "请先选择需要重新计算的报表！")
                        return;
                    }
                    var record = records[0];
                    Ext.Ajax.request({
                        url: 'report/calculate/recalculate',
                        params: {
                            reportId: record.get('id')
                        },
                        success: function (response, opts) {
                            if (response.result.success) {
                                me.getStore().reload();
                                Extend.Msg.info("提示", response.result.result, response.result.messages, response.result.errors)
                            }
                        }
                    });

                }
            },
            fillUp: {
                text: '补齐数据',
                handler: function () {
                    var me = this.up().up();
                    Ext.Ajax.request({
                        url: 'report/calculate/fill_up_kpis',
                        params: {
                            projectId: me.projectId,
                            reportConfigurationId: me.reportConfiguration.id
                        },
                        success: function (response, opts) {
                            me.getStore().reload();
                            Ext.Msg.alert("提示", response.result.result)
                        },
                        failure: function (response, opts) {
                            Ext.Msg.alert("提示", "补齐数据失败")
                        }
                    });
                }
            },
            quickCreate: {
                hidden: true
            },
            add: {
                hidden: true,
                before: ''
            }
        }
    },
    extraParams: function () {
        return {
            'projectId': this.projectId,
            reportConfigurationId: this.reportConfiguration.id
        }
    },
    bind: {
        store: '{Query}',
        search: '{search}'
    },
    editor: {
        formClazz: 'App.report.project.view.ProjectReportEditor',
        save: 'report/save',
        get: 'report/get_by_id',
        del: 'report/deletes',
        dataType: 'Report',
        model: 'App.report.model.ReportModel'
    },
    addHandler: function () {
        var project = app.project;
        this.editHandler(null, false, {
            parentType: 'project',
            reportConfigurationId: this.reportConfiguration.id
        });
    },
    editHandler: function () {
        this.editor.reportConfiguration = this.reportConfiguration;
        this.callParent(arguments);
    },
    initComponent: function () {
        var me = this;
        me.columns = [{
            header: 'ID',
            dataIndex: 'id',
            hidden: true
        }];
        if (me.reportConfiguration.kpis) {
            Ext.Array.each(me.reportConfiguration.kpis, function (kpi) {
                var kc = kpi.kpiConfiguration;
                var column = {
                    header: kc.field,
                    dataIndex: kc.field,
                    hidden: kc.hidden || false,
                    formatter: kc.formatter,
                    baselineValue: kpi.baselineValue,
                    minValue: kpi.minValue,
                    maxValue: kpi.maxValue
                };
                if (kc.dataType == 'int' || kc.dataType == 'float') {
                    column.xtype = 'KpiColumn';
                }
                me.columns.push(column);
            })
        }
        me.callParent(arguments);
    }
});